package com.xiaoxin.nmzp.controller;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.xiaoxin.nmzp.domain.NmzpResume;
import com.xiaoxin.nmzp.service.INmzpResumeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 简历管理Controller
 * 
 * @author xiaoxin
 * @date 2024-10-20
 */
@RestController
@RequestMapping("/nmzp/resume")
public class NmzpResumeController extends BaseController
{
    @Autowired
    private INmzpResumeService nmzpResumeService;

    /**
     * 查询简历管理列表
     */
    @PreAuthorize("@ss.hasPermi('nmzp:resume:list')")
    @GetMapping("/list")
    public TableDataInfo list(NmzpResume nmzpResume)
    {
        startPage();
        List<NmzpResume> list = nmzpResumeService.selectNmzpResumeList(nmzpResume);
        return getDataTable(list);
    }

    /**
     * 导出简历管理列表
     */
    @PreAuthorize("@ss.hasPermi('nmzp:resume:export')")
    @Log(title = "简历管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NmzpResume nmzpResume)
    {
        List<NmzpResume> list = nmzpResumeService.selectNmzpResumeList(nmzpResume);
        ExcelUtil<NmzpResume> util = new ExcelUtil<NmzpResume>(NmzpResume.class);
        util.exportExcel(response, list, "简历管理数据");
    }

    /**
     * 获取简历管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('nmzp:resume:query')")
    @GetMapping(value = "/{resumeId}")
    public AjaxResult getInfo(@PathVariable("resumeId") Long resumeId)
    {
        return success(nmzpResumeService.selectNmzpResumeByResumeId(resumeId));
    }

    /**
     * 新增简历管理
     */
    @PreAuthorize("@ss.hasPermi('nmzp:resume:add')")
    @Log(title = "简历管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NmzpResume nmzpResume)
    {
        return toAjax(nmzpResumeService.insertNmzpResume(nmzpResume));
    }

    /**
     * 修改简历管理
     */
    @PreAuthorize("@ss.hasPermi('nmzp:resume:edit')")
    @Log(title = "简历管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NmzpResume nmzpResume)
    {
        return toAjax(nmzpResumeService.updateNmzpResume(nmzpResume));
    }

    /**
     * 删除简历管理
     */
    @PreAuthorize("@ss.hasPermi('nmzp:resume:remove')")
    @Log(title = "简历管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{resumeIds}")
    public AjaxResult remove(@PathVariable Long[] resumeIds)
    {
        return toAjax(nmzpResumeService.deleteNmzpResumeByResumeIds(resumeIds));
    }

    /**
     * 简历上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public ResponseEntity<String> uploadLicense(@RequestParam("file") MultipartFile file,@RequestParam("resumeId") String resumeId) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body("文件为空");
        }

        try {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
            System.out.println("文件路径："+avatar);
            if (avatar != null) {
                int update = nmzpResumeService.updateAttachment(avatar,resumeId);
                return ResponseEntity.ok("文件上传成功: " + avatar);
            }
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败: " + e.getMessage());
        } catch (InvalidExtensionException e) {
            throw new RuntimeException(e);
        }
        return ResponseEntity.badRequest().body("文件格式不正确");
    }
    @GetMapping("/download")
    public ResponseEntity<Resource> downloadFile(@RequestParam String fileName) {
        try {
            // 文件存储的根目录，例如 /path/to/files/
            String fileBasePath = "http://localhost:8080";
            // 创建文件路径
            Path filePath = Paths.get(fileBasePath + fileName);
            Resource resource = new UrlResource(filePath.toUri());

            if (resource.exists() || resource.isReadable()) {
                return ResponseEntity.ok()
                        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
                        .body(resource);
            } else {
                return ResponseEntity.badRequest().body(null);
            }
        } catch (MalformedURLException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

}
